﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RainEngine.Rendering;

namespace RainEngine.Effects
{
    /// <summary>
    /// 高级地形使用的Effect类，它是对AdvancedTerrain.fx的封装。
    /// </summary>
    public class AdvancedTerrainEffect : DiffuseLightEffectWrapper
    {
        EffectParameter texture1Param;
        EffectParameter texture2Param;
        EffectParameter texture3Param;
        EffectParameter texture4Param;
        EffectParameter texture12UVTileParam;
        EffectParameter texture34UVTileParam;

        public override Vector3 EyePosition
        {
            set { }
        }

        /// <summary>
        /// 获取或设置第一张纹理
        /// </summary>
        public Texture2D Texture1
        {
            get { return texture1Param.GetValueTexture2D(); }
            set { texture1Param.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置第二张纹理
        /// </summary>
        public Texture2D Texture2
        {
            get { return texture2Param.GetValueTexture2D(); }
            set { texture2Param.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置第三张纹理
        /// </summary>
        public Texture2D Texture3
        {
            get { return texture3Param.GetValueTexture2D(); }
            set { texture3Param.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置第四张纹理
        /// </summary>
        public Texture2D Texture4
        {
            get { return texture4Param.GetValueTexture2D(); }
            set { texture4Param.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置第1、2张纹理的平铺次数，前两个分量为第1张纹理的平铺次数，后两个分量为第2张纹理的平铺次数，默认为Vector4(1,1,1,1)。
        /// </summary>
        public Vector4 Texture12UVTile
        {
            get { return texture12UVTileParam.GetValueVector4(); }
            set { texture12UVTileParam.SetValue(value); }
        }

        /// <summary>
        /// 获取或设置第3、4张纹理的平铺次数，前两个分量为第3张纹理的平铺次数，后两个分量为第4张纹理的平铺次数，默认为Vector4(1,1,1,1)。
        /// </summary>
        public Vector4 Texture34UVTile
        {
            get { return texture34UVTileParam.GetValueVector4(); }
            set { texture34UVTileParam.SetValue(value); }
        }        

        /// <summary>
        /// 创建一个新AdvancedTerrainEffect对象。
        /// </summary>
        public AdvancedTerrainEffect()
            : base(RainGE.AdvancedTerrainEffect )
        {
            
        }        

        /// <summary>
        /// 缓存effect的参数。
        /// </summary>
        protected override void CacheEffectParameters()
        {
            base.CacheEffectParameters();
            texture1Param = effect.Parameters["Texture1"];
            texture2Param = effect.Parameters["Texture2"];
            texture3Param = effect.Parameters["Texture3"];
            texture4Param = effect.Parameters["Texture4"];
            texture12UVTileParam = effect.Parameters["Texture12UVTile"];
            texture34UVTileParam = effect.Parameters["Texture34UVTile"];
        }

        public override void OnApply()
        {
            base.OnApply();
            
            // 重新设置shader index参数
            if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0)
            {
                int shaderIndex = 0;

                if (fogEnabled)
                    shaderIndex += 1;

                if (lightingEnabled)
                {
                    shaderIndex += 2;
                }

                shaderIndexParam.SetValue(shaderIndex);

                dirtyFlags &= ~EffectDirtyFlags.ShaderIndex;
            }
        }
    }
}